/*
 * test_main.c
 *
 *  Created on: 2011-3-9
 *      Author: chenl
 */

#include "logengine/logapi.h"
#include "datetime/datetime.h"
#include "timer/timermgt.h"
#include "common/common_delay.h"
#include "common/common_def.h"
#include "common/common_type.h"
#include "common/common_thread.h"
#include "common/common_bytequeue.h"
#include "common/common_magic.h"
#include "logengine/logqueue.h"
#include "logengine/buffedlog.h"
#include "logengine/logengine.h"
#include "logengine/simplelog.h"
#include "logengine/logthread.h"
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

static void test_log();
static void test_datetime();
static void test_timer();
static void test_thread();

class CTestThread : public CThread
{
public:
	virtual void Execute()
	{
		uint32_t n = 0;
		while (!bTerminated_)
		{
			if (0 == (++n) % 1000)
			{
				printf("test thread exec %d\n", n);
			}
			DELAY(1000);
		}
	}
};

void test_log()
{
	int i = 0;
	WriteLog(NULL, enmStderr, enmLogLevel_INFO, "testing logapi\n");
	while (1)
	{
		++i;
		WriteLog("./log/testlog_sync", enmSYNC, enmLogLevel_WARN,
				"#%04d test sync\n", i);
		WriteLog("./log/testlog_stderr", enmStderr, enmLogLevel_DEBUG,
				"#%04d test stderr\n", i);
		WriteLog("./log/testlog_stdio", enmStdIO, enmLogLevel_DEBUG,
				"#%04d test stdio\n", i);
		//		DELAY(1000000);
	}
}

void test_datetime()
{
	const char* cszTime = "2011-03-22 15:27:00";
	CDateTime t0(cszTime);
	int64_t t2 = CDateTime::CurrentDateTime().uSeconds();
	printf("t2 = %lld\n", t2);
	CDateTime t3(CDateTime::CurrentDateTime());
	char szTime[MAX_DATETIME_LEN + 1] =
	{ 0 };
	unsigned len = sizeof(szTime);
	if (0 < t3.ToString(szTime, len))
		return;
	assert(len == MAX_DATETIME_LEN);
}

void test_timer()
{
	CTimerMgt timerMgt;

	timerMgt.SetTimer(0, 1, 10 * US_PER_SECOND, false, 0, 0);
	timerMgt.SetTimer(0, 0, 3 * US_PER_SECOND, true, 0, 0);
	timerMgt.SetTimer(0, 2, 5 * US_PER_SECOND, true, 0, 0);
	timerMgt.SetTimer(0, 3, 17 * US_PER_SECOND, false, 0, 0);
	while (1)
	{
		CTimerMgt::TimerID timerID = timerMgt.GetFirstTimer();
		if (0 == timerID)
			goto LAB_DELAY;
		Timer timer;
		memset(&timer, 0, sizeof(timer));
		if (0 < timerMgt.GetTimer(timerID, timer))
		{
			fprintf(stderr, "get timer %d failed!\n", timerID);
			continue;
		}
		if (timer.nEndTime < CDateTime::CurrentDateTime().uSeconds())
		{
			printf(
					"timer %d expired! timer.nEndTime = %lld, CDateTime::CurrentDateTime().uSeconds() = %lld \n",
					timerID, timer.nEndTime, CDateTime::CurrentDateTime().uSeconds());
			if (0 < timerMgt.TimerFired(timerID))
				fprintf(stderr, "timerMgt.TimerFired(%d) failed!\n", timerID);
		}
LAB_DELAY:
	DELAY(1000);
	}
}

void test_thread()
{
	CTestThread thread;
	thread.Start();
	int n = 0;
	while(1)
	{
		if (0 == (++n) % 1000)
		{
			printf("main thread exec %d\n", n);
		}
		DELAY(1000);
		if (10000 == n)
		{
			thread.Terminate();
		}
	}
}

static void test_log2()
{
	CSimpleLog log();
}

static void test_bytequeue()
{
	CByteQueue<21>	testQueue;
	int32_t ret = testQueue.Initialize();
	if (0 > ret)
	{
		fprintf(stderr, "Initializing testQueue failed!ret=0x%08x\n", ret);
	}

	char szBuff[100] = {0};
	BuffParam buff = {0};
	buff.pBuff = (uint8_t*)szBuff;
	int i = 0;
	while(1)
	{
		sprintf(szBuff, "test queue #%03d", ++i);
		buff.len = strlen(szBuff);
		ret = testQueue.Push(buff);
		if (0 > ret)
		{
			fprintf(stderr, "Push testQueue failed! buff.pBuff=%s, buff.len=%d ret=0x%08x\n", buff.pBuff, buff.len, ret);
		}
		memset(buff.pBuff, DEBUF_MEM_MAGIC, sizeof(szBuff));
		ret = testQueue.Pop(buff);
		if (0 > ret)
		{
			fprintf(stderr, "Pop testQueue failed! buff.pBuff=%s, buff.len=%d ret=0x%08x\n", buff.pBuff, buff.len, ret);
		}
		szBuff[buff.len] = '\0';
		fprintf(stderr, "szBuff = %s\n", szBuff);
		DELAY(10000);
	}
}

enum
{
	enmLogSource_testbuffedlog = 0,
};

static void test_buffedlog()
{
	CLogQueue<1> logQueue;
	int32_t ret = logQueue.Initialize();

	if (0 > ret)
	{
		fprintf(stderr, "Initializing logQueue failed!ret=0x%08x\n", ret);
	}
	CBuffedLog<CLogQueue<1>, enmLogSource_testbuffedlog> logger(enmLogLevel_DEBUG, "./log/testbuffedlog", &logQueue);

	ret = logger.Initialize();
	if (0 > ret)
	{
		fprintf(stderr, "Initializing logger failed!ret=0x%08x\n", ret);
	}

	CLogThread<1> logThread(&logQueue);
	ret = logThread.Initialize();
	if (0 > ret)
	{
		fprintf(stderr, "Initializing logThread failed!ret=0x%08x\n", ret);
	}
	logThread.Start();
	int i = 0;
	while(1)
	{
		logger.WriteLog(enmLogLevel_DEBUG, "test buffed log! n=%d", i++);
		fprintf(stderr, "write buffed log n=%d\n", i);
		DELAY(10000);
	}
}

int main()
{
	//	test_log();
	//	test_datetime();
	//  test_timer();
	//	test_thread();
//	test_bytequeue();
	test_buffedlog();
}
